local xichun = fk.CreateSkill{
  name = "doublez__xichun"
}

Fk:loadTranslationTable{
  ["doublez__xichun"] = "曦春",
  [":doublez__xichun"] = "每回合限一次，当你成为基本牌或普通锦囊牌的目标时，"..
    "你可以将本回合弃牌堆中点数大于此牌和小于此牌的各一张牌交给当前回合角色，视为抵消此牌。",

  ["doublez__xichun_card-phase"] = "曦春",
  ["#doublez__xichun_active_prompt"] = "曦春：你可以令你可以将本回合弃牌堆中点数大于此牌和小于此牌的各一张牌交给 %dest，视为抵消此 %arg",

  ["$doublez__xichun1"] = "无论何时何地，我都在你身边。",
  ["$doublez__xichun2"] = "辗转流离，只为此刻与君相遇。",
}

xichun:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(xichun.name, true) then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(player.room.discard_pile, info.cardId) then
              return true
            end
          end
        end
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.DiscardPile and not table.contains(player.room.discard_pile, info.cardId) then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local ids = player:getTableMark("doublez__xichun-turn")
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(room.discard_pile, info.cardId) then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.DiscardPile and not table.contains(room.discard_pile, info.cardId) then
          table.removeOne(ids, info.cardId)
        end
      end
    end
    room:setPlayerMark(player, "doublez__xichun-turn", ids)
  end,
})

xichun:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local ids = {}
  room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
    for _, move in ipairs(e.data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(room.discard_pile, info.cardId) then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
    end
  end, Player.HistoryTurn)
  room:setPlayerMark(player, "doublez__xichun-turn", ids)
end)

xichun:addEffect(fk.TargetConfirming, {
  can_trigger = function (self, event, target, player, data)
    local discards = table.map(table.filter(player:getTableMark("doublez__xichun-turn"),  function (card)
      return player.room:getCardArea(card) == Card.DiscardPile
    end), Util.Id2CardMapper)
    return player == target and player:hasSkill(xichun.name)
      and player:usedSkillTimes(xichun.name, Player.HistoryTurn) < 1
      and data.card and (not data.card:isVirtual()) and (data.card.type == Card.TypeBasic or data.card:isCommonTrick())
      and table.find(discards, function(card) return card.number < data.card.number end)
      and table.find(discards, function(card) return card.number > data.card.number end)
  end,
  on_cost = function (self, event, target, player, data)
    local discards = table.map(player:getTableMark("doublez__xichun-turn"), Util.Id2CardMapper)
    local room = player.room
    room:setPlayerMark(player, "doublez__xichun_card", data.card.id)
    local done, dat = room:askToUseActiveSkill(player, {
      skill_name =  "#doublez__xichun_active",
      prompt = "#doublez__xichun_active_prompt::"..room.current.id..":"..data.card:toLogString(),
      extra_data = {
        skillName = xichun.name
      }
    })
    room:setPlayerMark(player, "doublez__xichun_card", 0)
    if done and dat then
      event:setCostData(self, {cards = dat.cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    room:obtainCard(room.current, cards, true, fk.ReasonPrey, player, xichun.name)
    room:setPlayerMark(player, "doublez__xichun_card-phase",  data.card.id)
  end
})

xichun:addEffect(fk.CardUseFinished, {
  can_refresh = function (self, event, target, player, data)
    return  player:hasSkill(xichun.name) and player:getMark("doublez__xichun_card-phase") == data.card.id
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "doublez__xichun_card-phase", 0)
  end
})

xichun:addEffect(fk.PreCardEffect, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(xichun.name) and data.to == player and player:getMark("doublez__xichun_card-phase") == data.card.id
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data.isCancellOut = true
    player.room:setPlayerMark(player, "doublez__xichun_card-phase", 0)
  end
})

local xichun_active = fk.CreateSkill{
  name = "#doublez__xichun_active"
}

xichun_active:addEffect("active", {
  expand_pile = function (self, player)
    return table.filter(player:getTableMark("doublez__xichun-turn"), function (card)
      return Fk:currentRoom():getCardArea(card) == Card.DiscardPile
    end)
  end,
  target_filter = Util.FalseFunc,
  feasible = function (self, player, selected, selected_cards, card)
    return #selected_cards == 2
  end,
  mute = true, 
  card_filter = function (self, player, to_select, selected, selected_targets)
    local discards = table.filter(player:getTableMark("doublez__xichun-turn"), function (card)
      return Fk:currentRoom():getCardArea(card) == Card.DiscardPile
    end)
    local card = Fk:getCardById(player:getMark("doublez__xichun_card"))
    if #selected >= 2 or not table.contains(discards, to_select) then
      return
    end
    if #selected == 0 then return Fk:getCardById(to_select).number ~= card.number end
    if Fk:getCardById(selected[1]).number > card.number then
      return Fk:getCardById(to_select).number < card.number
    else
      return Fk:getCardById(to_select).number > card.number
    end
  end
})

return {xichun, xichun_active}